/*
 *所有关于cw_djlc_wlzb类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*cw_djlc_wlzbService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Finance.IRepositories;
using VOL.Core.DBManager;
using System.Collections.Generic;
using System;

namespace VOL.Finance.Services
{
    public partial class cw_djlc_wlzbService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Icw_djlc_wlzbRepository _repository;//访问数据库

        [ActivatorUtilitiesConstructor]
        public cw_djlc_wlzbService(
            Icw_djlc_wlzbRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }

        public override WebResponseContent Update(SaveModel saveModel)
        {
    
            return base.Update(saveModel);
        }
        WebResponseContent webResponse = new WebResponseContent();

        public override WebResponseContent Import(List<IFormFile> files)
        {
            DownLoadTemplateColumns = x => new { x.materialnum, x.componentnum, x.parent_componentnum, x.MTyp, x.description, x.num, x.pricesource, x.unitprice };
            cw_djlc_bomnumber_view view = DjlcGetMaxCode.GetMaxCode();
            ImportOnExecuting = (List<cw_djlc_wlzb> list) =>
            {
                foreach (var item in list)
                {
                    item.bomnumber = view.bomnumber;
                    item.bomtype = 1;
                    item.is_delete = 0;//物料主表is_delete默认值0 20240103 新增
                }

                return webResponse.OK();
            };
            return base.Import(files);
        }

        /// <summary>
        /// 下载模板时，部分字段无需展示
        /// </summary>
        /// <returns></returns>
        public override WebResponseContent DownLoadTemplate()
        {
            DownLoadTemplateColumns = x => new { x.materialnum, x.componentnum,x.parent_componentnum, x.MTyp, x.description, x.num, x.pricesource, x.unitprice };
            return base.DownLoadTemplate();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public WebResponseContent Import1(List<IFormFile> files, string parameter)
        {
            DownLoadTemplateColumns = x => new { x.materialnum, x.componentnum, x.parent_componentnum, x.MTyp, x.description, x.num, x.pricesource, x.unitprice };
            cw_djlc_bomnumber_view view = DjlcGetMaxCode.GetMaxCode();
            ImportOnExecuting = (List<cw_djlc_wlzb> list) =>
            {
                foreach (var item in list)
                {
                    item.bomnumber = view.bomnumber;
                    item.bomtype = 1;
                    item.is_delete = 0;//物料主表is_delete默认值0 20240103 新增
                }

                return webResponse.OK();
            };
            
            cw_djlc_djzb cw_djlc_djzb = DBServerProvider.DbContext.Set<cw_djlc_djzb>().Where(x => x.cw_djlc_code == parameter).FirstOrDefault();
            cw_djlc_djzb.oldbomnumber = view.bomnumber;
            DBServerProvider.DbContext.Set<cw_djlc_djzb>().Update(cw_djlc_djzb);
            DBServerProvider.DbContext.SaveChanges();
            
            return base.Import(files);
        }

        public override PageGridData<cw_djlc_wlzb> GetPageData(PageDataOptions options)
        {
            QueryRelativeExpression = (IQueryable<cw_djlc_wlzb> queryable) =>
            {
                //物料主表逻辑删除 20240103 新增
                queryable = queryable.Where(x => x.is_delete == 0);
                return queryable;
            };
            return base.GetPageData(options);
        }

        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            WebResponseContent webResponse = new WebResponseContent();
            if (keys.IsNullOrEmpty())
            {
                return webResponse.Error("参数为空，请检查！");
            }
            using (var db = DBServerProvider.DbContext)
            {
                foreach (var key in keys)
                {
                    Guid wlzbId = Guid.Parse(key.ToString());
                    cw_djlc_wlzb wlzb = db.Set<cw_djlc_wlzb>().Where(x => x.cw_djlc_wlzb_id == wlzbId).ToList().FirstOrDefault();
                    wlzb.is_delete = 1;// 1删除  0未删除
                    db.Update(wlzb);
                }
                db.SaveChanges();
                
            }
            webResponse.Code = "-1";
            return webResponse.OK("删除成功！");
        }

        public override WebResponseContent Add(SaveModel saveDataModel)
        {
            WebResponseContent webResponse = new WebResponseContent();
            AddOnExecuting = (cw_djlc_wlzb order, object list) =>
            {
                order.is_delete = 0; //物料主表添加操作设置默认值 20240103 新增

                return webResponse.OK();
            };
            return base.Add(saveDataModel);
        }
    }
}
